﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Common;

namespace CIT.WMS.houseWork
{
    public partial class FrmInitStock : Form
    {
        public FrmInitStock()
        {
            InitializeComponent();
        }


        private void txt_location_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btn_ok_Click(null, null);
            }
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_barCode.Text.Trim()))
            {
                lbl_msg.Text = "条码不能为空";
                return;
            }
            if (string.IsNullOrEmpty(txt_Qty.Text.Trim()))
            {
                lbl_msg.Text = "数量不能为空";
                return;
            }
            try
            {
                int Qty = Convert.ToInt32(txt_Qty.Text.Trim());
                if (Qty <= 0)
                {
                    lbl_msg.Text = "数量只能为大于0的正整数";
                    return;
                }
                if (InsertToDBStock(txt_barCode.Text.Trim(), Qty, txt_location.Text.Trim()))
                {
                    lbl_msg.Text = "成功!";
                }
                else
                {
                    lbl_msg.Text = "失败";
                }
            }
            catch (Exception ex)
            {
                lbl_msg.Text = "数量只能为数字";
                return;
            }
        }

        private bool InsertToDBStock(string varBarCode, int varQty, string varLocation)
        {
            string varPartNum = string.Empty;
            string varDateCode = string.Empty;
            string varMsg = string.Empty;
            AnalysisBarCode.BeginAnalysis(varBarCode, out varPartNum, out varDateCode, out varMsg);
            if (varMsg != "OK")
            {
                lbl_msg.Text = varMsg;
                return false;
            }

            try
            {
                //数据库操作
                string strsql = string.Format("select *  from T_Bllb_StockInfo_tbsi where SerialNumber='{0}'", txt_barCode.Text.Trim());
                DataTable dtExits = Utils.NMS.QueryDataTable(Utils.uContext, strsql, new CIT.WMS.MESWcf.CmdParameter[0]);
                StringBuilder strBuilder = new StringBuilder();
                if (dtExits.Rows.Count > 0)//已经存在
                {
                    strBuilder.AppendFormat(@"update T_Bllb_StockInfo_tbsi set Location_SN='{0}',QTY='{1}',DateCode='{2}',MaterialCode='{3}' where SerialNumber='{4}'",
                        txt_location.Text.Trim(), txt_Qty.Text.Trim(), varDateCode, varPartNum, txt_barCode.Text.Trim());
                }
                else
                {
                    //不存在

                    strBuilder.AppendFormat(@"insert into dbo.T_Bllb_StockInfo_tbsi
        (
          Location_SN ,
          QTY ,
          In_Time ,
          SerialNumber ,
          MaterialCode,
          DateCode
        )
values  ( 
          '{0}' , -- Location_SN - nvarchar(50)
          '{1}' , -- QTY - int
          getDate() , -- In_Time - datetime
          '{2}' , -- SerialNumber - nvarchar(200)
           '{3}',
           '{4}'
        ) ", txt_location.Text.Trim(), txt_Qty.Text.Trim(), txt_barCode.Text.Trim(), varPartNum, varDateCode);
                }
                try
                {
                    bool flag = false;
                    bool result = false;
                    Utils.NMS.Exectransql(Utils.uContext, strBuilder.ToString(), new CIT.WMS.MESWcf.CmdParameter[0], out flag, out result);
                    txt_barCode.Text = string.Empty;
                    txt_location.Text = string.Empty;
                    txt_Qty.Text = string.Empty;
                    txt_barCode.Focus();
                    return true;

                }
                catch
                {

                    return false;
                }
            }
            catch (Exception ex)
            {
                lbl_msg.Text = ex.Message.ToString();
                return false;
            }
        }

        private void txt_barCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (string.IsNullOrEmpty(txt_barCode.Text.Trim()))
                {
                    lbl_msg.Text = "条码不能为空";
                    return;
                }
                lbl_msg.Text = string.Empty;
                txt_Qty.Focus();
            }
        }

        private void txt_Qty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (string.IsNullOrEmpty(txt_Qty.Text.Trim()))
                {
                    lbl_msg.Text = "数量不能为空";
                    return;
                }
                int Qty = Convert.ToInt32(txt_Qty.Text.Trim());
                if (Qty <= 0)
                {
                    lbl_msg.Text = "数量只能为大于0的正整数";
                    return;
                }
                lbl_msg.Text = string.Empty;
                txt_location.Focus();
            }
        }
    }
}